var P4 = function (game) {

  this.create = function () {
    ClosePage(game, '4_0', '4', '4_r', '4_w');

    function Page(game, cbg, mbg, rbg, wbg) {
      var flagAry = [0, 0, 0, 0];
      var bg = game.add.sprite(0, 0, mbg);
      //元素位置的计算原点
      bg.anchor.setTo(0, 0);
      var graphics = game.add.graphics(0, 0);
      var group = game.add.group();
      var bimgs = ['4_cof', '4_cof', '4_cof', '4_cof'];

      for (var i = 0; i < 4; i++) {
        var button = game.add.button(660 + i * 150, 370, imgs[bimgs[i]], actionOnClick, this, 2, 1, 0);
        button.data = i;
        group.add(button);
      }
      drawGraphics();

      function drawGraphics() {
        graphics.clear();
        for (var i = 0; i < 4; i++) {
          if (flagAry[i] === 1) {
            graphics.lineStyle(8, 0xE7C00B, 1);
          } else {
            graphics.lineStyle(4, 0x48486C, 1);
          }
          graphics.beginFill(0x48486C, 1);
          graphics.drawEllipse(710 + i * 150, 450, 50, 20);
        }
      }

      function actionOnClick(arg) {
        flagAry.forEach(function (item, i) {
          if (i != arg.data) {
            flagAry[i] = 0;
          }
        })
        flagAry[arg.data] = flagAry[arg.data] === 0 ? 1 : 0;
        drawGraphics();
      }


      var retry = game.add.button(1080, 625, imgs['button'], onSubmit, this, 2, 1, 0);
      retry.name = 'retry';
      retry.width = 160;
      retry.height = 55;
      //container.addChild(retry);

      var submit = game.add.button(1250, 625, imgs['button'], onSubmit, this, 2, 1, 0);
      submit.name = 'submit';
      submit.width = 160;
      submit.height = 55;

      //container.addChild(submit);

      function onSubmit(arg) {
        //container.destroy();
        if (arg.name === 'submit') {
          if (flagAry.join('_') === '0_0_1_0') {
            RightPage(game, cbg, mbg, rbg, wbg);
          } else {
            WrongPage(game, cbg, mbg, rbg, wbg);
          }
        } else {
          game.goState('back');
        }
      }
    }

    function ClosePage(game, cbg, mbg, rbg, wbg) {
      var container = game.add.sprite(0, 0, '');
      var close = game.add.button(0, 0, cbg, onClose, this, 2, 1, 0);
      container.addChild(close);

      function onClose() {
        container.destroy();
        Page(game, cbg, mbg, rbg, wbg);
      }
    }

      function WrongPage(game,cbg,mbg,rbg,wbg){
          var container=game.add.sprite(0,0,'');

          var bg = game.add.image(0, 0, wbg);
          container.addChild(bg);
          var retry = game.add.button(1250, 625, imgs['button'], onClick, this, 2, 1, 0);
          retry.width = 160;
          retry.height = 55;
          container.addChild(retry);

          function onClick() {
              container.destroy();
              Page(game, cbg, mbg, rbg, wbg);
          }
      }

    function RightPage(game, cbg, mbg, rbg, wbg) {
      var container = game.add.sprite(0, 0, '');

      var bg = game.add.image(0, 0, rbg);
      container.addChild(bg);
      var next = game.add.button(1250, 625, imgs['button'], onClick, game, 2, 1, 0);
      next.width = 160;
      next.height = 55;
      container.addChild(next);

      function onClick() {
        game.goState('next');
      }
    }
  }


}





